package com.gree.changying;

import android.app.Application;
import android.content.Context;

import com.gree.changying.server.BaseAction;
import com.gree.changying.server.db.DbHelper;
import com.gree.changying.server.task.TaskManager;
import com.gree.changying.server.utils.NLog;
import com.gree.changying.server.utils.SharedPreferencesUtil;
import com.gree.changying.utils.DESUtil;
import com.gree.changying.utils.FileUtil;
import com.gree.changying.utils.StringUtil;
import com.hjq.permissions.Permission;

public class App extends Application {

    public static  String appId ;
    public Context mContext;
    private static App app;
    //用于全局的位置信息
    public static String addresss;
    public static String addressName;
    //纬度
    public static double latitude;
    //经度
    public static double longitude;
    //用于加密的安全密钥
    public static String DESKEY = "";
    public static final String[] allpermission = new String[]{
            Permission.READ_EXTERNAL_STORAGE,
            Permission.WRITE_EXTERNAL_STORAGE,
            Permission.ACCESS_COARSE_LOCATION,
            Permission.ACCESS_FINE_LOCATION
    };
    public static String getLonLat(){
        //暂时保留6位
        return String.format("%.6f", longitude)+","+String.format("%.6f", latitude);
    }
    public static String getLongitude(){
        return String.format("%.6f", longitude);
    }
    public static String getLatitude(){
        return String.format("%.6f", latitude);
    }
    public static Application getInstance() {
        return app.getApplication();
    }
    private Application getApplication(){
        return  this;
    }
    public static App getApp() {
        return app;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        appId = getPackageName();
        NLog.i("appId",appId);
        mContext = getApplicationContext();
        //数据库初始化

        DbHelper.getInstance(mContext);
        FileUtil.getInstance(mContext);
        //配置工具初始化
        SharedPreferencesUtil.getInstance(getApplicationContext(),Const.SP_NAME);
        //加解密安全密钥
        DESKEY = getResources().getString(R.string.DESkey);
        TaskManager.getInstance().init(20);
        String currentUser = SharedPreferencesUtil.getData(Const.USERID,null);
        String currentPsd =  SharedPreferencesUtil.getData(Const.USERPSD,null);
        if(!StringUtil.isEmpty(currentUser)&&!StringUtil.isEmpty(currentPsd)){
            BaseAction.init(currentUser, DESUtil.decryptor(currentPsd));
        }
        if(Const.DEVELOPMENT){
            Const.EVENT = SharedPreferencesUtil.getData(Const.EVENT_VAL,2);
        }
    }

    /**
     * 获取全局字体大小
     * @return
     */
    public float getFontScale(){
        int currentIndex= SharedPreferencesUtil.getData(Const.CURRENT_FONT_SIZE,1);
        return 1+(currentIndex*0.08f-0.08f);
    }
}
